package cn.com.gridinfo.sooc.core.login.activity.forgetpwd;

import android.os.Bundle;

import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.ForgetDao;
import cn.com.gridinfo.sooc.utils.MD5;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 重置密码
 * Created by 刘晓晨 on 2015/12/4.
 */
public class ResetPwdActivity extends BaseSoocActivity {
    @InjectView(R.id.newPassWord)
    ClearEditText newPassWord;//新密码
    @InjectView(R.id.send)
    Button send;// 提交
    private String xtdz;//拿到传来的节点域名
    private String uid;//拿到用户uid
    private ForgetDao dao;// 用户dao
    private String newPwd;// 定义全局 新密码
    private String type = "";//定义找回方式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_pwd_activity);
        SoocApplication.getContext().addActivity(this);
        ButterKnife.inject(this);
        dao = new ForgetDao(this);
        xtdz = getIntent().getStringExtra("xtdz");//得到上个界面传来的节点域名
        uid = getIntent().getStringExtra("uid");//得到上个界面传来的用户uid
        type = getIntent().getStringExtra("type");//拿到找回方式
        Log.d("xtdz" + xtdz);
        Log.d("uid" + uid);
        Log.d("type" + type);
        dao.setXtdz(xtdz);
        send.setEnabled(false);//初始状态按钮不可点击
        newPassWord.setImeOptions(EditorInfo.IME_ACTION_DONE);
        newPassWord.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (validateNotNull()) {
                        sendNewPwd();
                    }

                }
                return false;
            }
        });

    }

    private boolean validateNotNull() {
        if (StringUtils.isNull(newPassWord.getText().toString().trim())) {
            newPassWord.requestFocus();
            return false;
        }
        return true;
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "重置密码";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //ToolBar的back键
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    //点击确定
    @OnClick(R.id.send)
    void sendNewPwd() {
        newPwd = newPassWord.getText().toString();
        if (newPwd.length() < 6 || newPwd.length() > 20) {
            MessageUtils.showImageShortToast(this, "请输入6~20位密码~");
        } else if (newPwd.contains(" ")) {
            MessageUtils.showImageShortToast(this, "用户密码不能包含空格~");
        } else {
            dao.forgetPwd(uid, newPwd, type, uid + MD5.encode(StringUtils.changeSpecialChar(newPwd)) + type + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.NEWPASSWORD://修改密码请求成功
                if (dao.status.equals("0")) {
                    MessageUtils.showRightImageShortToast(this, "密码重置成功");
                    new CountDownTimer(1000, 1000) {//延时一秒后进行销毁一组Activity
                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            //一秒后，销毁一组activity 重新进入Login页面
                            SoocApplication.getContext().exit();
                        }
                    }.start();

                } else {//失败 提示信息
                    MessageUtils.showRightImageShortToast(this, dao.statusCode);
                }
                break;
        }


    }


    //监听输入框状态,有内容时可点击
    @OnTextChanged(value = R.id.newPassWord, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onTextChanged() {
        if (StringUtils.isNull(newPassWord.getText().toString())) {
            send.setEnabled(false);
        } else {
            send.setEnabled(true);
        }
    }
}
